package com.bluesmart.mia;

import java.util.List;
import java.util.Locale;

import android.R.color;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.Toast;

import com.sys.DemoApp;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.umeng.update.UmengUpdateAgent;
import com.util.PreferencesUtils;

@SuppressWarnings("deprecation")
public class MainTabActivity extends TabActivity implements OnCheckedChangeListener
{
	static public MainTabActivity mMainTabActivity;
	int mType = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintabs);
		
		mMainTabActivity = this;
        initUnit();
        
        try
		{
			mType = getIntent().getExtras().getInt("type");
		}catch(Exception e){}
		
		List<Activity> list = ((DemoApp)this.getApplicationContext()).getActivityList();
        int count = list.size();
        for(int i = count - 1; i >= 0; i--)
        {
        	list.get(i).finish();
        }

		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);
		UmengUpdateAgent.setDeltaUpdate(false);

		((RadioButton) findViewById(R.id.radio_button0))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button1))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button2))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button3))
				.setOnCheckedChangeListener(this);
		setupIntent();

		// mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);
		
		((RadioButton) findViewById(R.id.radio_button0)).setChecked(true);
		((RadioButton) findViewById(R.id.radio_button0)).setBackgroundColor(Color.parseColor("#0D80D6"));
		
		UmengUpdateAgent.update(this);
        PushAgent mPushAgent = PushAgent.getInstance(this);
        mPushAgent.enable();
        mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);
	}
	
	public int getType()
	{
		return mType;
	}
	
	void initUnit()
	{
		Integer unithabit = PreferencesUtils.getInt(this, "unithabit");
		if(unithabit != null) ((DemoApp) getApplicationContext()).glbUnit = unithabit;
		else
		{
			Locale locale = getResources().getConfiguration().locale;
	        String language = locale.getLanguage();
	        if (language.endsWith("zh")) ((DemoApp) getApplicationContext()).glbUnit = 2;
	        else ((DemoApp) getApplicationContext()).glbUnit = 1;
		}
	}
	
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			
			((RadioButton) findViewById(R.id.radio_button0)).setBackgroundColor(color.transparent);
			((RadioButton) findViewById(R.id.radio_button1)).setBackgroundColor(color.transparent);
			((RadioButton) findViewById(R.id.radio_button2)).setBackgroundColor(color.transparent);
			((RadioButton) findViewById(R.id.radio_button3)).setBackgroundColor(color.transparent);
			
			switch (buttonView.getId()) {
			case R.id.radio_button0:
				this.getTabHost().setCurrentTabByTag("A_TAB");
				((RadioButton) findViewById(R.id.radio_button0)).setChecked(true);
				((RadioButton) findViewById(R.id.radio_button0)).setBackgroundColor(Color.parseColor("#0D80D6"));
				break;
			case R.id.radio_button1:
				this.getTabHost().setCurrentTabByTag("B_TAB");
				((RadioButton) findViewById(R.id.radio_button1)).setChecked(true);
				((RadioButton) findViewById(R.id.radio_button1)).setBackgroundColor(Color.parseColor("#0D80D6"));
				break;
			case R.id.radio_button2:
				this.getTabHost().setCurrentTabByTag("C_TAB");
				((RadioButton) findViewById(R.id.radio_button2)).setChecked(true);
				((RadioButton) findViewById(R.id.radio_button2)).setBackgroundColor(Color.parseColor("#0D80D6"));
				break;
			case R.id.radio_button3:
				this.getTabHost().setCurrentTabByTag("D_TAB");
				((RadioButton) findViewById(R.id.radio_button3)).setChecked(true);
				((RadioButton) findViewById(R.id.radio_button3)).setBackgroundColor(Color.parseColor("#0D80D6"));
				break;
			}
		}
	}

	private void setupIntent() {
		getTabHost().addTab(
				buildTabSpec("A_TAB",R.string.mia_name, R.drawable.btn_tab0,new Intent(this,MainNoticeActivity.class)));
		
		getTabHost().addTab(
				buildTabSpec("B_TAB", R.string.mia_name, R.drawable.btn_tab2,
						new Intent(this, MainPageActivity.class)));
		getTabHost().addTab(
				buildTabSpec("C_TAB",R.string.mia_name, R.drawable.btn_tab1,
						new Intent(this, NoteViewActivity.class)));
		getTabHost().addTab(
				buildTabSpec("D_TAB", R.string.mia_name, R.drawable.btn_tab3,
						new Intent(this, ChatViewAtivity.class)));
	}
	
	public void setCurrentTab(int index)
	{
		getTabHost().setCurrentTab(index);
		
		((RadioButton) findViewById(R.id.radio_button0)).setBackgroundColor(color.transparent);
		((RadioButton) findViewById(R.id.radio_button1)).setBackgroundColor(color.transparent);
		((RadioButton) findViewById(R.id.radio_button2)).setBackgroundColor(color.transparent);
		((RadioButton) findViewById(R.id.radio_button3)).setBackgroundColor(color.transparent);
		
		((RadioButton) findViewById(R.id.radio_button0 + index)).setChecked(true);
		((RadioButton) findViewById(R.id.radio_button0 + index)).setBackgroundColor(Color.parseColor("#0D80D6"));
	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return getTabHost()
				.newTabSpec(tag)
				.setIndicator(getString(resLabel),
						getResources().getDrawable(resIcon))
				.setContent(content);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {// 捕捉返回键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitApp();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private long exitTime = 0;
	public void exitApp() {
		if ((System.currentTimeMillis() - exitTime) > 2000) 
		{
			Toast.makeText(this, "再按一次退出 应用", Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
		} 
		else
		{
			finish();
		}
	}
}
